package com.example.iot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.iot.mapper.LineMapper;
import com.example.iot.mapper.MeterReadingMapper;
import com.example.iot.pojo.IotDataObj;
import com.example.iot.pojo.Line;
import com.example.iot.pojo.Meter_Reading;
import com.example.iot.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author : Lin
 * @version : [v1.0]
 * @className : ImgsController
 * @description : 描述说明该类的功能
 * @createTime : 2023/10/8 12:36
 * @updateUser : Lin
 * @updateTime : 2023/10/8 12:36
 * @updateRemark : 描述说明本次修改内容
 */

@RestController
public class ImgsController {

    @Autowired
    LineMapper lineMapper;
    @Autowired
    private MeterReadingMapper meterReadingMapper;

    @Value("${upload.directory}")
    private String abspath;
    @Value("${upload.reflectdirectory}")
    private String repath;
    private final String meter_reading_folderPath="meter_reading_imgs/";
    private final String line_folderPath="line_imgs/";


    @RequestMapping(value = "/reportLineDetection" ,method = RequestMethod.POST)
    public R reportLineDetection(@RequestPart("image") MultipartFile file) throws IOException {
        String filename=abspath+line_folderPath+file.getOriginalFilename();
        System.out.println(filename);
        byte[] bytes = file.getBytes();
        FileOutputStream fileOutputStream = new FileOutputStream(filename);
        fileOutputStream.write(bytes);
        fileOutputStream.close();

        String url=repath+line_folderPath+file.getOriginalFilename();
        Line line = new Line();
        line.setImgurl(url);
        line.setIsdeal(0);
        lineMapper.insert(line);


//        System.out.println(url);
        LambdaQueryWrapper<Line> lqw = new LambdaQueryWrapper<Line>();
        List<Line> lines = lineMapper.selectList(lqw);
        return R.ok(lines);
    }

    @RequestMapping(value = "/getLineDetections",method = RequestMethod.POST)
    public R getLineDetections(){
        LambdaQueryWrapper<Line> lqw = new LambdaQueryWrapper<Line>();
        lqw.orderByDesc(Line::getIsdeal);
        List<Line> lines = lineMapper.selectList(lqw);
        System.out.println(lines);
        return R.ok(lines) ;
    }

    @RequestMapping(value = "/changeLineDetections" ,method = RequestMethod.GET)
    public R changeLineDetections(@PathParam("isdeal") Integer isdeal,@PathParam("id") Integer id){
        if (!ObjectUtils.isEmpty(isdeal) && !ObjectUtils.isEmpty(id)){
            if (isdeal==1){
                //isdeal==1  处理 ==》 未处理
                Line line = lineMapper.selectById(id);
                line.setIsdeal(0);
                line.setStatus("严重破损");
                lineMapper.updateById(line);

                LambdaQueryWrapper<Line> lqw = new LambdaQueryWrapper<Line>();
                List<Line> lines = lineMapper.selectList(lqw);
                return R.ok(lines);
            }else {
//                isdeal==0   未处理 ==》 处理

                Line line = lineMapper.selectById(id);
                line.setIsdeal(1);
                line.setStatus("已处理");
                lineMapper.updateById(line);


                LambdaQueryWrapper<Line> lqw = new LambdaQueryWrapper<Line>();

                List<Line> lines = lineMapper.selectList(lqw);
                return R.ok(lines);
            }
        }
        return R.ok(null);

    }

    @RequestMapping(value = "/getMeterReadings" ,method = RequestMethod.POST)
    public R getMeterReadings(){
        LambdaQueryWrapper<Meter_Reading> lqw = new LambdaQueryWrapper<Meter_Reading>();
        List<Meter_Reading> meter_readings = meterReadingMapper.selectList(lqw);
        return R.ok(meter_readings) ;
    }



    public void InitFolder(){
        // 创建File对象，表示文件夹
        File meter_reading_folder = new File(meter_reading_folderPath);
        File line_folder = new File(line_folderPath);
        // 判断文件夹是否存在
        if (!meter_reading_folder.exists()) {
            // 文件夹不存在，尝试创建它
            boolean created = meter_reading_folder.mkdirs();

            if (created) {
                System.out.println(meter_reading_folder+"文件夹创建成功！");
            } else {
                System.err.println(meter_reading_folder+"文件夹创建失败！");
            }
        } else {
            System.out.println(meter_reading_folder+"文件夹已经存在。");
        }

        if (!line_folder.exists()) {
            // 文件夹不存在，尝试创建它
            boolean created = line_folder.mkdirs();

            if (created) {
                System.out.println(line_folder+"文件夹创建成功！");
            } else {
                System.err.println(line_folder+"文件夹创建失败！");
            }
        } else {
            System.out.println(line_folder+"文件夹已经存在。");
        }
    }
}
